
import { _decorator, AnimationClip, Component, Animation, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum';
import State from '../../Base/State';
import { StateMachine, getInitParamsNumber, getInitParamsTrigger } from '../../Base/StateMachine';

import { EntityManager } from '../../Base/EntityManager';


const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerState
 * DateTime = Wed Jun 28 2023 09:28:04 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = PlayerState.ts
 * FileBasenameNoExtension = PlayerState
 * URL = db://assets/Script/Player/PlayerState.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

//状态机实现



const BASE_URL = 'texture/burst'


@ccclass('BurstStateMachine')
export class BurstStateMachine extends StateMachine {


    //开始初始化他们了
    async init() {

        this.animationsComponent = this.addComponent(Animation) //拆
        // this.animationsComponent.playOnLoad=true

        this.initParams() //初始参数
        this.initStateMachine() //初始状态机
        this.initAnimationEvent()


        await Promise.all(this.waitingList)
    }


    //动画事件  吧动画变为ido
    initAnimationEvent() {
        // this.animationsComponent.on(Animation.EventType.FINISHED, () => {
        //     const name = this.animationsComponent.defaultClip.name
        //     const whiteList = ['attack']
        //     if (whiteList.some(v => name.includes(v))) {
        //         this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
        //         // this.setParams(PARAMS_NAME_ENUM.IDLE, true)
        //     }
        // })
    }


    initParams() {  //添加参数默认值？
        this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber())
        this.params.set(PARAMS_NAME_ENUM.DEATH, getInitParamsTrigger())





        //BLOCKRONT
    }


    initStateMachine() {
        // //定义idle动画
        // //new一个state?  返回什么？
        // this.stateMachines.set(PARAMS_NAME_ENUM.IDLE,new State(this,"texture/player/idle/top",AnimationClip.WrapMode.Loop))

        //定义turnleft动画
        // this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT,new State(this,"texture/player/turnleft/top"))


        this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new State(this,`${BASE_URL}/idle`))

        this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new State(this,`${BASE_URL}/death`))
        this.stateMachines.set(PARAMS_NAME_ENUM.ATTACK, new State(this,`${BASE_URL}/attack`))



    }


    //判断当前状态
    run() {
        //判断当前的状态
        switch (this.currentState) {

            case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
            case this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK):
                case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):

                if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
                }else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)
                } else if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
                    
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK)
                } else {
                    this.currentState = this.currentState
                }
                break;
            default: this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)

        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
